package org.jeecg.modules.charge.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.SsoUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.entity.charge.ChargeBills;
import org.jeecg.entity.charge.ChargeInvoice;
import org.jeecg.entity.charge.ChargeInvoiceInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.charge.mapper.ChargeBillsMapper;
import org.jeecg.modules.charge.mapper.ChargeInvoiceInfoMapper;
import org.jeecg.modules.charge.mapper.ChargeInvoiceMapper;
import org.jeecg.modules.charge.service.IChargeInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Description: 票据详情
 * @Author: jeecg-boot
 * @Date:   2019-08-26
 * @Version: V1.0
 */
@Service
@Transactional(readOnly = false)
public class ChargeInvoiceInfoServiceImpl extends ServiceImpl<ChargeInvoiceInfoMapper, ChargeInvoiceInfo> implements IChargeInvoiceInfoService {

    @Autowired
    private ChargeInvoiceInfoMapper chargeInvoiceInfoMapper;
    @Autowired
    private ChargeInvoiceMapper chargeInvoiceMapper;
    @Autowired
    private ChargeBillsMapper chargeBillsMapper;

    public Page<ChargeInvoiceInfo> getOtherInvoice(Page<ChargeInvoiceInfo> page,ChargeInvoiceInfo chargeInvoiceInfo){
        return  page.setRecords(chargeInvoiceInfoMapper.getOtherInvoice(page, chargeInvoiceInfo));
    }
    public Integer insertInfos(List<ChargeInvoiceInfo> infolist){
        return chargeInvoiceInfoMapper.insertInfos(infolist);
    }
    public Page<ChargeInvoiceInfo> getDepositList(Page<ChargeInvoiceInfo> page,ChargeInvoiceInfo chargeInvoiceInfo){
       List<ChargeInvoiceInfo> list =  chargeInvoiceInfoMapper.getDepositList(page, chargeInvoiceInfo);
        if(list.size() > 0){
            for (ChargeInvoiceInfo info : list){
                List<ChargeInvoiceInfo> children = new ArrayList<ChargeInvoiceInfo>();
                if(StringUtils.isNotEmpty(info.getDepositId())){
                    ChargeInvoiceInfo historyBills = chargeInvoiceInfoMapper.get(info.getDepositId());
                    children.add(historyBills);
                }
                if(children.size() > 0 ){
                    info.setChildren(children);
                }
            }
        }
        return  page.setRecords(list);
    }

    /**根据ID查询票据详情*/
    public ChargeInvoiceInfo get(String id){
        return chargeInvoiceInfoMapper.get(id);
    }

    public Integer refundUpdate(String id){
        return chargeInvoiceInfoMapper.refundUpdate(id);
    }
    public Integer depositRefund(ChargeInvoice chargeInvoice){
        LoginUser loginUser = SsoUtil.getLoginUser();
        ChargeInvoiceInfo invoInfo = chargeInvoiceInfoMapper.get(chargeInvoice.getId());
        Integer winOrFail = 0;
        // 添加退款票据表信息
        ChargeInvoice invoice = new ChargeInvoice();
        if (invoInfo != null) {
            winOrFail += chargeInvoiceInfoMapper.refundUpdate(chargeInvoice.getId()); //票据详情表退款
            String fpid = chargeInvoiceMapper.getIdByInvoice(chargeInvoice.getInvoiceHm(), chargeInvoice.getInvoiceDm());
            invoice = chargeInvoiceMapper.get(invoInfo.getInvoiceId());//获取押金信息
            invoice.setMoney(0 - invoInfo.getMoney());// 退款的金额
            invoice.setPayMethod(chargeInvoice.getPayMethod());
            invoice.setZjSkrq(chargeInvoice.getZjSkrq());
            invoice.setRemarks("押金退款");
            invoice.setInvoiceDm(chargeInvoice.getInvoiceDm());
            invoice.setInvoiceHm(chargeInvoice.getInvoiceHm());
            invoice.setUpdateDate(new Date());
            invoice.setUpdateBy(loginUser.getId());
            invoice.setId(fpid);
            winOrFail += chargeInvoiceMapper.updateInvoices(invoice);
            // 添加票据详情表信息
            invoInfo.setDepositId(chargeInvoice.getId());
            invoInfo.setId(String.valueOf(IdWorker.getId()));
            invoInfo.setMoney(0 - invoInfo.getMoney());
            invoInfo.setRealMoney(0 - invoInfo.getRealMoney());
            invoInfo.setRealPrice(0 - invoInfo.getRealPrice());
            invoInfo.setPayMethod(chargeInvoice.getPayMethod());
            invoInfo.setZjSkrq(chargeInvoice.getZjSkrq());
            invoInfo.setInvoiceId(fpid);// 票据id添加到票据详情表
            invoInfo.setInvoiceDm(chargeInvoice.getInvoiceDm());
            invoInfo.setInvoiceHm(chargeInvoice.getInvoiceHm());
            invoInfo.setIsDeposit("2");//退款押金标识
            invoInfo.setRemarks(chargeInvoice.getRemarks());
            List<ChargeInvoiceInfo> list = new ArrayList<ChargeInvoiceInfo>();
            list.add(invoInfo);
            winOrFail += chargeInvoiceInfoMapper.insertInfos(list);
        }
        return winOrFail;
    }
    public Integer  depositDelete(ChargeInvoice chargeInvoice){
        LoginUser loginUser = SsoUtil.getLoginUser();
        Integer winOrFail = 0;
        if(StringUtils.isNotEmpty(chargeInvoice.getId())){
            ChargeInvoiceInfo info = chargeInvoiceInfoMapper.get(chargeInvoice.getId());
            ChargeInvoice invoice = chargeInvoiceMapper.get(info.getInvoiceId());
            invoice.setScrapDate(chargeInvoice.getScrapDate());
            invoice.setScrapDes(chargeInvoice.getScrapDes());
            invoice.setScrapMan(loginUser.getRealname());

            ChargeBills bills = new ChargeBills();
            bills.setInvoiceGrnno(chargeInvoice.getInvoiceDm());
            bills.setInvoiceNo(chargeInvoice.getInvoiceHm());
            List<ChargeBills> billsList = chargeBillsMapper.selectBillsAll(bills);
            if(billsList.size() > 0){
                for(ChargeBills bill:billsList) {
                    bill.setZjSkrq(null);
                    bill.setPayuserId(null);
                    bill.setPayuserName(null);
                    bill.setZjTkrq(null);
                    bill.setPayType(null);
                    bill.setInvoiceNo(null);
                    bill.setInvoiceGrnno(null);
                    bill.setMakeinvoiceDate(null);
                    bill.setIncome(null);
                    bill.setTaxPaid(0.0);
                    bill.setTaxRate(0.0);
                    winOrFail += chargeBillsMapper.updateyOneUtily(bill);
                }
            }
            winOrFail= chargeInvoiceMapper.scrap(invoice);
        }
        return winOrFail;
    }

    public Page<ChargeInvoiceInfo> getDepositReportList(Page<ChargeInvoiceInfo> page,ChargeInvoiceInfo chargeInvoiceInfo){
        if(chargeInvoiceInfo.getFeeCodes() !=null && chargeInvoiceInfo.getFeeCodes().size() > 0 && StringUtils.isEmpty(chargeInvoiceInfo.getFeeCodes().get(0))){
            chargeInvoiceInfo.setFeeCodes(null);
        };
        List<ChargeInvoiceInfo> list =  chargeInvoiceInfoMapper.getDepositList(page, chargeInvoiceInfo);
        return  page.setRecords(list);
    }

    public List<ChargeInvoiceInfo> getDepositReportList(ChargeInvoiceInfo chargeInvoiceInfo){
        List<ChargeInvoiceInfo> list =  chargeInvoiceInfoMapper.getDepositReportList(chargeInvoiceInfo);
        return  list;
    }

}
